package com.ljkj.cordial.chat.ui.views;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.base.Dto;
import com.lanjiu.lib.imclient.dto.user.RegisteredUserDto;
import com.lanjiu.lib.imclient.dto.user.UserDto;
import com.lanjiu.lib.utils.log.Logger;
import com.ljkj.cordial.chat.ui.IView;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/11/20 10:42
 */
public class LoginView extends IView {

    private LoginCallback mCallback;

    public LoginView(LoginCallback mCallback) {
        this.mCallback = mCallback;
    }

    public void doRegisteredLogin(String userId, String password) {
        RegisteredUserDto registeredUser = new RegisteredUserDto();
        registeredUser.setUserId(userId);
        registeredUser.setSecret(password);
        doSendRequest(EventConstants.Action.LOGIN_REGISTERED, registeredUser.toJson());
    }

    @Override
    public void onMessageEventHandle(MessageEvent event) {
        Logger.print("LoginView", event.toString());
        if (EventConstants.Action.LOGIN_REGISTERED == event.action()) {
            UserDto user = null;
            if (checkIsSuccess(event.code(), event.data())) {
                RegisteredUserDto registeredUser = Dto.fromJson(event.data(), RegisteredUserDto.class);
                if (null != registeredUser) user = new UserDto(registeredUser);
            }
            if (null != mCallback)
                mCallback.onLoginCallback(event.code(), event.msg(), user);
        }
    }

    public interface LoginCallback {

        void onLoginCallback(int code, String msg, UserDto data);
    }
}
